<?php

namespace app\admin\model;

use think\facade\Request;
use think\Model;

class Goods extends Model{
    // 搜索器
    public  function searchGoodsNameAttr($query,$value)
    {
        if (!''==$value){
            $query->where('goods_name','like','%'.$value.'%');
        }
    }
    public function searchMaxTimeAttr($query,$value)
    {
        if (!empty($value)){
            $query->whereTime('add_time','>=',$value);
        }
    }
    public function searchMinTimeAttr($query,$value)
    {
        if (!empty($value)){
            $query->whereTime('add_time','<=',$value);
        }
    }
    public function searchMaxQianAttr($query,$value)
    {
        if (!empty($value)){
            $query->where('price','>=',$value);
        }
    }
    public function searchMinQianAttr($query,$value)
    {
        if (!empty($value)){
            $query->where('price','<=',$value);
        }
    }
    public function searchRecommendedAttr($query,$value)
    {
        if (isset($value) && is_scalar($value) && '' != $value){
            $query->where('ecm_goods.recommended',strval($value));
        }
        if (isset($value) && is_array($value) && count($value) >0){
            $query->whereIn('ecm_goods.recommended',$value);
        }
    }
    public function searchIfShowAttr($query,$value)
    {
        if (isset($value) && is_scalar($value) && '' != $value){
            $query->where('ecm_goods.if_show',strval($value));
        }
        if (isset($value) && is_array($value) && count($value) >0){
            $query->whereIn('ecm_goods.if_show',$value);
        }

    }

    public function searchIfOpenAttr($query,$value)
    {
        if (isset($value) && is_scalar($value) && '' != $value){
            $query->where('ecm_goods.if_open',strval($value));
        }
        if (isset($value) && is_array($value) && count($value) >0){
            $query->whereIn('ecm_goods.if_open',$value);
        }
    }
    public function searchCateIdAttr($query,$value)
    {
        if (!empty($value)){
            $query->where('ecm_goods.cate_id',$value);
        }
    }

    public function searchOrderAttr($query,$value)
    {
        if ('desc' == $value){
            $query->order('add_time','desc');
        }elseif ('asc' ==$value){
            $query->order('add_time');
        }else{
            $query->order('goods_id');
        }
    }


    // 获取器
    public function getAddTimeAttr($value)
    {
        return date('Y-n-j H:i:s',$value);
    }
    public function getRecommendedAttr($value)
    {
        return $value?'推荐':'不推荐';
    }

    public function getIfShowAttr($value)
    {
        return $value?'热门':'不热门';
    }

    public function getIfOpenAttr($value)
    {
        return $value?'下架':'不下架';
    }

    public static function golist($data=[],$query=[],$page=true){
        $model = self::field("ecm_goods.*,ecm_brand.brand_name,ecm_gcategory.cate_name")
            ->withSearch(array_keys($data),$data)
            ->join("ecm_brand",'ecm_goods.brand=ecm_brand.brand_id',"LEFT")
            ->join("ecm_gcategory","ecm_goods.cate_id=ecm_gcategory.cate_id","LEFT");
        if($page){
            return $model->paginate([
                'list_rows'=>10,
                'query'=>$query
            ])->each(function ($v,$k){
                $v['goods_name'] = str_replace(Request::get('keywords'),"<span style='color: red;font-weight: bolder;'>".Request::get('keywords')."</span>",$v['goods_name']);
                return $v;
            });
        }
        return $model->select();

    }
}